
import time
import threading

g_num = 0

# lock
mutex = threading.Lock()

def test1(temp):
    global g_num
    mutex.acquire()
    for i in range(temp):
        g_num += 1
    mutex.release()
    print("...test1...%s" % g_num)

def test2(temp):
    global g_num
    for i in range(temp):
        mutex.acquire()
        g_num += 1
        mutex.release()
    print("...test2...%s" % g_num)

def main():
    t1 = threading.Thread(target=test2,args=(500000,))
    t2 = threading.Thread(target=test2,args=(500000,))

    t1.start()
    t2.start()

    print("...mian...%s" % g_num)

    time.sleep(5)

    print("...mian...%s" % g_num)

if __name__ == '__main__':
    main()
